<?php global $has_permit; ?>
<div class="db_left bl">
    <?php $this->load->view('cms/article/side_block'); ?>
</div>
<div class="db_right bl">
<?php if ($has_permit) :?>
    <div id="breadcrumb" class="clearfix mt_10">
        <ul class="h_list no_list_style ah">
            <li><a href="<?php echo site_url('admin/dashboard', true); ?>">Bảng điều khiển</a></li>
            <li class="SecondLast"><a href="<?php echo site_url('admin/cms/article/index', true); ?>">Quản lý bài viết</a></li>
        </ul>
    </div>
    <div class="std_header auto_height">
        <div class="left">
            <strong>Biên tập bài viết</strong>
        </div>
        <div class="right">
            <button type="button" class="gray_btn" onclick="window.location='<?php echo site_url('admin/cms/article/index', true); ?>'"><span>Trở lại danh sách</span></button>
            <?php if ($is_admin) :?>
            &nbsp;
            <button class="gray_btn" type="button" onclick="return admin_cms_article.save_go_back();"><span>Lưu và quay lại</span></button>
            <?php endif; ?>
            &nbsp;
            <button class="gray_btn" type="submit" onclick="$('#frm_article').submit();"><span>Lưu và tiếp tục</span></button>
            <?php if (isset($article->id)) :?>
            &nbsp;
            <button type="button" onclick="admin_cms_article.confirm_delete(this);" title="Bạn muốn xóa bài viết <?php echo get_field($article, 'name'); ?>?" action="<?php echo site_url('admin/cms/article/delete/id/' . $article->id, true); ?>" class="gray_btn" type="submit"><span>Xóa bài viết</span></button>
            <?php endif; ?>
            &nbsp;
            <button class="gray_btn" onclick="window.location='<?php echo site_url('admin/cms/article/edit', true); ?>'">
                <span>Thêm bài mới</span>
            </button>
        </div>
    </div>
    <div class="mt_10">
        <?php $this->load->view('cms/article/message'); ?>
    </div>
    <div id="article_management">
    <form id="frm_article" enctype="multipart/form-data" action="<?php echo site_url('admin/cms/article/save', true); ?>" method="post">
        <input type="hidden" id="go_back" name="go_back" value="0" />
        <?php if (isset($article->id)) :?>
        <input type="hidden" name="id" value="<?php echo $article->id; ?>" />
            <?php endif; ?>
        <input type="hidden" name="status" value="<?php echo get_field($article, 'status'); ?>" />
        <div id="tab_store" class="tab_container">
        <ul>
            <li id="active_general"><a href="#tab_general"><span>Thông tin bài viết</span></a></li>
            <li id="active_category"><a href="#tab_category"><span>Chọn chuyên mục</span></a></li>
            <li id="active_seo"><a href="#tab_seo"><span>Thông tin SEO</span></a></li>
            <li id="active_related"><a href="#tab_related"><span>Bài viết liên quan</span></a></li>
        </ul>
        <div id="tab_general" style="padding-top: 0px !important">
            <div class="form_block">
                <div class="block_header">
                    Thông tin bài viết <?php if (!empty($article->id)) :?><strong>[<?php echo $article->title; ?>]</strong><?php endif; ?>
                </div>
                <div class="block_content">
                    <table cellpadding="0px" cellspacing="0px">
                        <?php if (!empty($article->id)) :?>
                        <tr>
                            <td class="frm_label">Tình trạng bài viết</td>
                            <td class="frm_field">
                                <strong><span class="simple_btn"><?php echo $this->cms_article_model->get_status_label($article->status); ?></span></strong>
                            </td>
                        </tr>
                        <tr>
                            <td class="frm_label">ID bài viết(*)</td>
                            <td class="frm_field">
                                <strong><span class="simple_btn">#<?php echo $article->id; ?></span></strong>
                            </td>
                        </tr>
                        <tr>
                            <td class="frm_label">Thời điểm cập nhật(*)</td>
                            <td class="frm_field">
                                <span><?php echo render_date($article->created_at, 'd/m/y, H:i'); ?> - <?php echo render_date($article->updated_at, 'd/m/y, H:i'); ?></span>
                            </td>
                        </tr>
                        <?php endif; ?>
                        <tr>
                            <td class="frm_label">Nguồn tin(*)</td>
                            <td class="frm_field">
                                <input placeholder="Nguồn tin" type="text" name="data[source]" value="<?php echo get_field($article, 'source'); ?>" title="Nguồn tin không được để trống" class="std_input required" />
                            </td>
                        </tr>
                        <tr>
                            <td class="frm_label">Tiêu đề bài viết(*)</td>
                            <td class="frm_field">
                                <input placeholder="Tiêu đề tin" type="text" name="data[title]" value="<?php echo get_field($article, 'title'); ?>" title="Tiêu đề bài viết cần phải nhập" class="std_input required" />
                            </td>
                        </tr>
                        <tr>
                            <td class="frm_label">Alias(*)</td>
                            <td class="frm_field">
                                <input placeholder="Bí danh tin" id="article_alias" type="text" name="data[alias]" value="<?php echo get_field($article, 'alias'); ?>" class="std_input" />
                            </td>
                        </tr>
                        <tr>
                            <td class="frm_label">Thiết lập(*)</td>
                            <td class="frm_field">
                                <div class="bl">
                                    <label for="sort_order">Thứ tự bài</label>&nbsp;
                                    <input name="data[sort_order]" value="<?php echo get_field($article, 'sort_order'); ?>" id="sort_order" type="text" class="std_input" style="width:50px !important; min-width: 50px !important;" />
                                </div>
                                <div class="bl" style="margin-left: 10px;">
                                    <input <?php if (intval(get_field($article, 'top_home')) == 1) :?>checked="checked"<?php endif; ?> id="top_home" style="top: 2px; position: relative" type="checkbox" name="data[top_home]" value="1" />
                                    <label for="top_home">Đầu trang chủ</label>
                                </div>
                                <div class="bl" style="margin-left: 10px;">
                                    <input <?php if (intval(get_field($article, 'top_category')) == 1) :?>checked="checked"<?php endif; ?> id="top_category" style="top: 2px; position: relative" type="checkbox" name="data[top_category]" value="1" />
                                    <label for="top_category">Đầu chuyên mục</label>
                                </div>
                                <div class="bl" style="margin-left: 10px;">
                                    <input <?php if (intval(get_field($article, 'hot_article')) == 1) :?>checked="checked"<?php endif; ?> id="hot_article" style="top: 2px; position: relative" type="checkbox" name="data[hot_article]" value="1" />
                                    <label for="hot_article">Tin tiêu điểm</label>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td class="frm_label">Ảnh đại diện(*)</td>
                            <td class="frm_field">
                                <?php $image = get_field($article, 'image'); ?>
                                <?php if (!empty($image)) :?>
                                <a target="_blank" href="<?php echo render_src($image); ?>"><img src="<?php echo render_src($image); ?>" width="40px" style="vertical-align: middle; border: 2px solid #bbb" /></a>
                                <?php endif; ?>
                                <input type="file" name="data_image" />
                                &nbsp;
                                <input onclick="return admin_cms_article.toggle_display_wp_img_path(this.checked)" style="top: 2px; position: relative" type="checkbox" name="set_img_url" id="set_img_url" value="1" />
                                <label for="set_img_url">Chọn đường dẫn ảnh</label>
                                <div class="clearfix mt_10" id="wp_img_path" style="display: none">
                                    <input name="img_path" type="text" class="std_input" value="<?php echo $image; ?>" />
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td class="frm_label">Kiểu tin(*)</td>
                            <td class="frm_field">
                                <select name="data[type]" style="width: 200px;">
                                    <?php foreach ($this->cms_article_model->get_list_types() as $type => $label) :?>
                                    <option value="<?php echo $type; ?>" <?php if (get_field($article, 'type') == $type) :?>selected="selected"<?php endif; ?>><?php echo $label; ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td class="frm_label">Xuất bản(*)</td>
                            <td class="frm_field">
                                <input <?php if (intval(get_field($article, 'status')) == 1) :?>checked="checked"<?php endif; ?> id="article_status" style="top: 2px; position: relative" type="checkbox" name="data[status]" value="1" />
                                <label for="article_status">Cho phép xuất bản</label>
                            </td>
                        </tr>
                        <tr>
                            <td class="frm_label">Thời điểm xuất bản tin(*)</td>
                            <td class="frm_field">
                                <div class="clearfix auto_height">
                                    <div class="bl">
                                        <strong><label>Ngày xuất bản: </label></strong>
                                    </div>
                                    <div class="bl ml_5">
                                        <input placeholder="Ngày xuất bản" name="data[publish_day]" type="text" class="std_input date_picker required" value="<?php echo render_date(get_field($article, 'publish_day')); ?>" />
                                    </div>
                                </div>
                                <div class="clearfix auto_height mt_10">
                                    <div class="bl ml_5">
                                        <strong><label>&nbsp;Giờ xuất bản: </label></strong>
                                    </div>
                                    <div class="bl ml_5">
                                        <input title="Giờ xuất bản không được để trống" placeholder="Giờ xuất bản" name="data[publish_hour]" type="text" class="std_input short_input required" value="<?php echo get_field($article, 'publish_hour'); ?>" />
                                    </div>
                                    <div class="bl ml_5">
                                        <input title="Phút xuất bản không được để trống" placeholder="Phút xuất bản" name="data[publish_minute]" type="text" class="std_input short_input required" value="<?php echo get_field($article, 'publish_minute'); ?>" />
                                    </div>
                                    <div class="bl ml_5">
                                        <input title="Giây xuất bản không được để trống" placeholder="Giây xuất bản" name="data[publish_second]" type="text" class="std_input short_input required" value="<?php echo get_field($article, 'publish_second'); ?>" />
                                    </div>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td class="frm_label">Mô tả ngắn(*)</td>
                            <td class="frm_field">
                                <textarea title="Mô tả ngắn cho bài viết không được để trống" name="data[description]" class="required"><?php echo get_field($article, 'description'); ?></textarea>
                            </td>
                        </tr>
                        <tr>
                            <td class="frm_label">Thẻ(*)</td>
                            <td class="frm_field">
                                <textarea name="data[tags]" class=""><?php echo get_field($article, 'tags'); ?></textarea>
                            </td>
                        </tr>
                        <tr>
                            <td class="frm_field" colspan="2">
                                <textarea title="Nội dung bài viết không được để trống" name="data[content]" class="editor"><?php echo get_field($article, 'content'); ?></textarea>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
        <div id="tab_category" style="padding-top: 0px !important">
            <div class="form_block">
                <div class="block_header">
                    Chọn chuyên mục cho bài viết <?php if (!empty($article->id)) :?><strong>[<?php echo $article->title; ?>]</strong><?php endif; ?>
                </div>
                <div class="block_content">
                    <table class="gridview" width="100%" cellpadding="0" cellspacing="0" border="0">
                        <thead>
                        <tr>
                            <td width="10px"><input id="chk_category_all" type="checkbox" onclick="btn_util.check_all(this.checked, '.chk_cat')" /></td>
                            <td><label for="chk_category_all">Chuyên mục</label></td>
                        </tr>
                        </thead>
                        <tbody>
                        <?php foreach ($categories as $category) :?>
                            <?php if (is_string($article->category_ids)) {$article->category_ids = explode(',', $article->category_ids);} ?>
                            <?php $article->category_ids = (is_array($article->category_ids)) ? $article->category_ids : array(); ?>
                        <tr>
                            <td>
                                <input id="chk_category_<?php echo $category->id; ?>" <?php if (in_array($category->id, $article->category_ids)) :?>checked="checked"<?php endif; ?> type="checkbox" class="chk_cat" id="chk_cat_<?php echo $category->id; ?>" name="data[category_ids][]" value="<?php echo $category->id; ?>" />
                            </td>
                            <td>
                                <?php $category_level = substr_count($category->path, '/') - 2; ?>
                                <?php for ($level = 0; $level < $category_level; $level++) :?>
                                <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                <?php endfor; ?>
                                <label <?php if ($category_level <= 0) :?>style="font-weight: bolder"<?php endif; ?> for="chk_category_<?php echo $category->id; ?>"><?php echo $category->name; ?></label>
                            </td>
                        </tr>
                            <?php endforeach; ?>
                        </tbody>
                        <tfoot>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
        <div id="tab_seo" style="padding-top: 0px !important">
            <div class="form_block">
                <div class="block_header">
                    Thông tin SEO <?php if (!empty($article->id)) :?><strong>[<?php echo $article->title; ?>]</strong><?php endif; ?>
                </div>
                <div class="block_content">
                    <table cellpadding="0px" cellspacing="0px">
                        <tr>
                            <td class="frm_label">Tiêu đề(*)</td>
                            <td class="frm_field">
                                <input type="text" name="data[meta_title]" value="<?php echo get_field($article, 'meta_title'); ?>" class="std_input" />
                            </td>
                        </tr>
                        <tr>
                            <td class="frm_label">Từ khóa</td>
                            <td class="frm_field">
                                <input type="text" name="data[meta_keywords]" value="<?php echo get_field($article, 'meta_keywords'); ?>" class="std_input" />
                            </td>
                        </tr>
                        <tr>
                            <td class="frm_label">Mô tả</td>
                            <td class="frm_field">
                                <input type="text" name="data[meta_description]" value="<?php echo get_field($article, 'meta_description'); ?>" class="std_input" />
                            </td>
                        </tr>
                        <tr>
                            <td class="frm_label">Index</td>
                            <td class="frm_field">
                                <select name="data[meta_index]" style="width: 200px">
                                    <?php foreach ($indexes as $index) :?>
                                    <option value="<?php echo $index; ?>" <?php if (get_field($article, 'meta_index') == $index) :?>selected="selected"<?php endif; ?>><?php echo $index; ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
        <div id="tab_related" style="padding-top: 0px !important">
            <div class="form_block">
                <div class="block_header">
                    Chọn tin liên quan cho bài viết
                </div>
                <div class="clearfix">
                    <ul class="article_related" id="article_related">
                        <?php if (!empty($article->related_layout)) :?>
                            <?php foreach ($article->related_layout as $related) :?>
                            <li id='related_id_<?php echo $related->id; ?>'><span title='Xóa khỏi danh sách liên quan' class='icon icon_delete' onclick='admin_cms_article.remove_related(<?php echo $related->id; ?>)'></span><span class='icon icon_preview' onclick='admin_cms_article.preview_article(<?php echo $related->id; ?>)'></span><a target='_blank' href='<?php echo render_site_url($related->alias); ?>'><?php echo $related->title; ?></a><input type='hidden' name='data[related_ids][]' value='<?php echo $related->id; ?>' /></li>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </ul>
                </div>
                <div class="block_content" id="filter_related_list">
                    <table class="gridview" width="100%" cellpadding="0" cellspacing="0" border="0">
                        <thead>
                        <tr>
                            <td><b>ID</b></td>
                            <td><b>Chọn</b></td>
                            <td colspan="3"><b>Tiêu đề</b></td>
                            <td colspan="5"><b>Tiêu chí khác</b></td>
                        </tr>
                        </thead>
                        <tbody>
                        <tr class="filters">
                            <td colspan="2">
                                <input placeholder="ID" type="text" class="bind_enter_evt small_filter" name="filters[ca.id][value]" title="Tìm kiếm theo id bài viết" onchange="return admin_cms_article.act_filter_related('filter_related_list')" />
                                <input type="hidden" value="equal" name="filters[ca.id][type]" />
                            </td>
                            <td colspan="2">
                                <input placeholder="Từ khóa" type="text" class="bind_enter_evt wide_filter" name="filters[title][value]" title="Tìm kiếm theo tiêu đề bài viết" onchange="return admin_cms_article.act_filter_related('filter_related_list')" />
                                <input type="hidden" value="text" name="filters[title][type]" />
                            </td>
                            <td>
                                <select style="width: 125px" onchange="return admin_cms_article.act_filter_related('filter_related_list')" placeholder="Tất cả" name="filters[user_id][value]">
                                    <option value="-1">-- Tất cả --</option>
                                    <?php foreach ($users as $user) :?>
                                        <option value="<?php echo $user->id; ?>"><?php echo get_field($user, 'username'); ?></option>
                                    <?php endforeach; ?>
                                </select>
                                <input type="hidden" value="equal" name="filters[user_id][type]" />
                                <input type="hidden" value="-1" name="filters[user_id][exclude]" />
                            </td>
                            <td>
                                <select style="width: 125px" onchange="return admin_cms_article.act_filter_related('filter_related_list')" placeholder="Tất cả" name="filters[ca.category_ids][value]">
                                    <option value="-1">-- Tất cả --</option>
                                    <?php foreach ($categories as $cat) :?>
                                    <?php if (intval($cat->parent_id) > 0) :?>└--<?php endif; ?>
                                    <option <?php if (isset($filters['ca.category_ids']['value']) && intval($filters['ca.category_ids']['value']) == intval($cat->id)) :?>selected="selected"<?php endif; ?> value="<?php echo get_field($cat, 'id'); ?>"><?php echo get_field($cat, 'name'); ?></option>
                                    <?php endforeach; ?>
                                </select>
                                <input type="hidden" value="text" name="filters[ca.category_ids][type]" />
                                <input type="hidden" value="-1" name="filters[ca.category_ids][exclude]" />
                            </td>
                            <td width="150px" colspan="2">
                                <button class="gray_btn" type="button" onclick="return admin_cms_article.act_filter_related('filter_related_list')">
                                    <span>Lọc</span>
                                </button>
                                <button class="gray_btn" type="button" onclick="return admin_cms_article.reset_filter_related('filter_related_list')">
                                    <span>Xóa lọc</span>
                                </button>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="9" style="padding: 10px">
                                <p align="center">Thực hiện chức năng lọc phía trên để tìm những tin liên quan đến bài viết</p>
                            </td>
                        </tr>
                        </tbody>
                        <tfoot>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
        </div>
        <button type="button" class="gray_btn" onclick="window.location='<?php echo site_url('admin/cms/article/index', true); ?>'"><span>Trở lại</span></button>
        <?php if ($is_admin) :?>
        &nbsp;
        <button class="gray_btn" type="button" onclick="return admin_cms_article.save_go_back()"><span>Lưu và quay lại</span></button>
        <?php endif; ?>
        &nbsp;
        <button class="gray_btn" type="submit" onclick="$('#frm_article').submit();"><span>Lưu và tiếp tục</span></button>
        <?php if (isset($article->id)) :?>
        &nbsp;
        <button type="button" onclick="admin_cms_article.confirm_delete(this);" title="Bạn muốn xóa bài viết <?php echo get_field($article, 'name'); ?>?" action="<?php echo site_url('admin/cms/article/delete/id/' . $article->id, true); ?>" class="gray_btn"><span>Xóa bài viết</span></button>
        &nbsp;
            <?php endif; ?>
        <br/><br/>
    </form>
    </div>
<?php else :?>
    <?php $this->load->view('layouts/restricted'); ?>
<?php endif; ?>
</div>
<div class="clearfix"></div>
<script type="text/javascript">
    $("#frm_article").validate({
       errorLabelContainer: $("#form_error")
    });
    var article_id = <?php if (isset($article->id)) :?><?php echo $article->id; ?><?php else :?>0<?php endif; ?>;
    $(".bind_enter_evt").each(function() {
        $(this).bind('keypress', function(e) {
            var code = (e.keyCode ? e.keyCode : e.which);
            if (code == 13) {
                $(this).trigger("change");
            } else {
                return true;
            }
            return false;
        });
    });
</script>
<script type="text/javascript" src="<?php echo BASE_URL; ?>js/admin/cms/article/function.js"></script>
